package com.aos.erp.common.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.aos.erp.common.utils.DateUtils;
import com.aos.erp.common.utils.JsonUtils;
import com.aos.erp.common.web.error.AosJsonErrorAttributes;
import com.aos.erp.common.web.filter.TokenFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
public class AosWebMvcConfigurer implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0, fastJsonConverter());
    }

    @Bean
    public HttpMessageConverter fastJsonConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(JsonUtils.serializerFeature);
        fastJsonConfig.setSerializeFilters(JsonUtils.serializeFilters);
        fastJsonConfig.setSerializeConfig(JsonUtils.serializeConfig);

        converter.setFastJsonConfig(fastJsonConfig);

        //只处理json的
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_STREAM_JSON);

        converter.setSupportedMediaTypes(supportedMediaTypes);

        return converter;
    }

    @Bean
    public Converter<String, Date> dateConverter() {
        return new Converter<String, Date>() {
            @Override
            public Date convert(String stringDate) {
                Date date = null;
                if (stringDate == null || stringDate.trim().length() == 0) {
                    date = null;
                } else {
                    //根据不同的格式自动转换,后期实现
                    date = DateUtils.toDate(stringDate);
                }
                return date;
            }
        };
    }


    @Bean
    public AosJsonErrorAttributes fastErrorAttributes() {
        return new AosJsonErrorAttributes();
    }


    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
        corsConfiguration.addAllowedHeader("*"); // 2允许任何头
        corsConfiguration.addAllowedMethod("*"); // 3允许任何方法（post、get等）
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }

    @Bean
    public TokenFilter tokenFilter() {
        return new TokenFilter();
    }

}

